import argparse
import pysam

def check_bam_pairs(bam_file):
    try:
        # 打开 BAM 文件
        bam = pysam.AlignmentFile(bam_file, "rb")
        
        # 计数器，用于限制只处理前三条读取
        count = 0
        for read in bam:
            if count >= 3:
                break
            if not read.has_tag('MD'):
                continue
            count += 1
            
            # 获取读取名称和 get_aligned_pairs() 结果
            read_name = read.query_name
            pairs = read.get_aligned_pairs(with_seq=True)  # 包含参考碱基
            
            # 输出结果
            print(f"Read: {read_name}")
            print(f"get_aligned_pairs(): {pairs}")
            print("-" * 50)
        
        bam.close()
        
        if count == 0:
            print("No reads found in the BAM file.")
            
    except Exception as e:
        print(f"Error processing BAM file: {e}")

def main():
    # 设置命令行参数
    parser = argparse.ArgumentParser(description="Output get_aligned_pairs() for the first three reads in a BAM file.")
    parser.add_argument("bam_file", help="Path to the input BAM file")
    args = parser.parse_args()
    
    # 检查 BAM 文件
    check_bam_pairs(args.bam_file)

if __name__ == "__main__":
    main()